/**
 * 伪造IP，下载图片
 * @type {_|exports|module.exports}
 * @private
 */
var _ = require('underscore');
var cheerio = require('cheerio');
var superagent = require('superagent');
var async = require('async');
var mkdirp = require('mkdirp');
var fs = require('fs');
var http = require('http');
var request = require("request");

//本地存储目录
var dir = './file_icons_vs_3';

//创建目录
mkdirp(dir, function (err) {
    if (err) {
        console.log(err);
    }
});

var rootUrl = "http://findicons.com";
//var url = "http://findicons.com/pack/5/animals?q=Search+in+icon+pack..."; //头像
var url = "http://findicons.com/pack/1636/file_icons_vs_3"; //头像

getData(url);

function getData(url) {

    console.log("url", url)

    superagent.get(url).end(function (err, sres) {
        if (err) {
            console.log(err)
            return;
        }

        var $ = cheerio.load(sres.text);

        //获取下个页面的url
        var pages_all = $(".pages");
        var thisPage = pages_all.find(".current");
        var next = thisPage.next("a");

        //页面上图片个数
        var icon_list = $(".icon_list");
        var dds = icon_list.find("dd");
        var pages = new Array();
        //可以下载的图片
        var imgs = new Array();

        _.each(dds, function (item, index) {

            var href = $(item).find("a").eq(0).attr("href");
            pages.push(href);

            var imgScr = $(item).find("a").eq(0).find("img").attr("src");
            imgs.push(imgScr);

        })

        console.log("需要遍历的面试个数:" , pages.length)
        console.log("可以下载的图片个数:" , imgs.length)

        /**
        //下载子页面的操作
        async.eachSeries(pages, function (item, callback) {


            getHtml(item, function () {
                callback(null)
            });

        }, function (err, result) {

            if (err) {
                console.log("-一轮结束-" + err)
            }
        });
         **/

        //直接下载图片
        async.eachSeries(imgs, function (item, callback) {

            //截取文件名
            var name = item.substr(item.lastIndexOf("/") + 1);

            saveImagea(item,  name, function () {
                callback( false);
            })

        }, function (err, result) {

            if (err) {
                console.log("-一轮结束-" + err)
            }
            var href = next.attr("href");
            if (href) {
                href = rootUrl + href;
                getData(href);
            }
        });


    });

}



function getHtml(href, getHtmlCB) {

    var temp = rootUrl + href;

    console.log(temp)

    superagent.get(temp).end(function (err, sres) {
        if (err) {
            console.log(err)
            return;
        }

        var $ = cheerio.load(sres.text);

        //获取图片名字
        var nameSrc = $(".tag_btn").find("a").eq(0).attr("title");
        var name = nameSrc;
        if (name) {
            name = name.replace(/ /g, "_")
        }

        //获取默认图片大小
        var size = $(".detial_list").find("li").eq(3).find("em").html();//128 x 128

        size = size.substr(0 , size.indexOf(" "));

        var con_tab_1_lis = $(".detail_button").find("li");
        _.each(con_tab_1_lis , function(li , index){
            var a = $(li).find("a");
            if(a.attr("title") == "Download PNG"){

                var href = a.attr("href");
                //下载原图
                saveImagea(href,  name + ".png", function () {
                })

                //下载原图
                //saveImagea1(href,  name + ".png")


                /**
                //下载图片
                var href1x = href.replace("/" + size + "/" , "/" + 32 + "/")
                saveImagea(href1x,  name + ".png", function () {
                })

                var href2x = href.replace("/" + size + "/" , "/" + 64 + "/")
                saveImagea(href2x, name + "@2x.png", function () {
                })

                var href3x = href.replace("/" + size + "/" , "/" + 96 + "/")
                saveImagea(href3x, name + "@2x.png", function () {
                })
                **/
            }
        });

        /**
        var imgs = $(".other-size").find("img");
        //console.log(imgs);

        //获取图片url,进行下载
        _.each(imgs,function(img , index){
            if(index < 3){
                var src = $(img).attr("src");
                var tempName = name;
                if(index == 0){
                    tempName += "@3x.png"
                }else if(index == 1){
                    tempName += "@2x.png"
                }else if(index == 2){
                    tempName += ".png"
                }
                saveImagea(src, dir, tempName, function () {
                })
            }

        })
         **/

        getHtmlCB()

    });

}


/**
 * 伪造ip，下载图片
 * @param url
 * @param name
 * @param cb
 */
var saveImagea = function (url, name, cb) {

    var ip = _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)

    console.log(url, ip)
    superagent
        .get(url)
        //.set("X-Forwarded-For" , "10.111.198.90")
        .set("X-Forwarded-For", ip)
        .end(function (err, sres) {
            if (err) {
                console.error(err)
                return;
            }
            // console.log(sres);
            fs.writeFile(dir + "/" + name, sres.body, 'binary', function (err) {
                if (err) throw err;
                console.log('file saved ' + name);
                cb();
            });
        })

}


//下载方法
function saveImagea1(url, filename){

    var ip = _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)
    var dowmloadUrl = rootUrl + url;
    console.log(dowmloadUrl, ip , filename)
    request(
    {
        method : 'GET',
        url : rootUrl + url,
        "X-Forwarded-For" : ip,
        headers: {
            'User-Agent': 'request',
            "x-forwarded-for" : ip,
            "remoteAddress" : ip,
            "x-real-ip" : ip
        }
    }, function(err, res, body){
        request(rootUrl + url).pipe(fs.createWriteStream(dir + "/" + filename));
    });
};
